//
//  HJPostHomePageController.m
//  KaiChat
//
//  Created by lx on 2024/3/27.
//

#import "HJPostHomePageController.h"
#import "HMSegmentedControl.h"
#import "HJPostContentController.h"

@interface HJPostHomePageController ()<UIScrollViewDelegate>

@property (nonatomic, weak) HMSegmentedControl *segmentedControl;
@property (nonatomic, strong) NSArray *types;
@property (nonatomic, strong) UIScrollView *scrollView;


@end


@implementation HJPostHomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIView *segmentedView = [UIView addToView:self.view];
    [segmentedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.offset(0);
        make.height.offset(50);
    }];
    
    UIView *lineView = [UIView addToView:segmentedView];
    lineView.backgroundColor = UIColor.tj_separatorColor;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(0);
        make.height.offset(0.7);
        make.bottom.offset(0);
    }];
    
    NSMutableArray *segmentedDatas = @[].mutableCopy;
    for (NSDictionary *dict in self.types) {
        [segmentedDatas addObject:dict[@"text"]];
    }
    
    HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:segmentedDatas];
    segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 15, 0, 15);
    segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationBottom;
    segmentedControl.selectionIndicatorHeight = 1;
    segmentedControl.selectionIndicatorEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    segmentedControl.selectionIndicatorColor = UIColor.tj_333333Color;
    segmentedControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleDynamic;
    segmentedControl.verticalDividerEnabled = NO;
    segmentedControl.backgroundColor = UIColor.clearColor;
    [segmentedControl setTitleTextAttributes:@{
        NSForegroundColorAttributeName : UIColor.tj_BEBDCAColor,
        NSFontAttributeName : UIFontMake(16)
    }];
    [segmentedControl setSelectedTitleTextAttributes:@{
        NSForegroundColorAttributeName : UIColor.tj_333333Color,
        NSFontAttributeName : UIFontMediumMake(16)
    }];
    [segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    [segmentedView addSubview:segmentedControl];
    [segmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(10);
        make.bottom.offset(0);
        make.height.offset(50);
    }];
    _segmentedControl = segmentedControl;
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, KNavBarHight + 50, KScreenWidth, KScreenHeight - KNavBarHight - 50)].addTo(self.view);
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.delegate = self;
    _scrollView.backgroundColor = UIColor.whiteColor;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * self.types.count, _scrollView.frame.size.height);
 
    [self.types enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        HJPostContentController *listController = obj[@"controller"];
        listController.type = [obj[@"type"] intValue];
        listController.userId = self.userId;
        [self addChildViewController:listController];
        [self.scrollView addSubview:listController.view];
        listController.view.frame = CGRectMake(KScreenWidth * idx, 0, KScreenWidth, _scrollView.tj_height);
    }];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    
    [self.segmentedControl setSelectedSegmentIndex:page animated:YES];
}


- (NSArray *)types {
    if (!_types) {
        _types = @[
            @{
                @"type": @"2",
                @"text": @"Post",
                @"controller": [[HJPostContentController alloc] init]
            },
            @{
                @"type": @"0",
                @"text": @"Reply",
                @"controller": [[HJPostContentController alloc] init]
            },
            @{
                @"type": @"1",
                @"text": @"Like",
                @"controller": [[HJPostContentController alloc] init]
            },
        ];
    }
    return _types;
}

- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl {
    [self.scrollView setContentOffset:CGPointMake(KScreenWidth * segmentedControl.selectedSegmentIndex, 0)];
}

@end
